<template>
  <div class="goods-contain">
    <!-- 面包屑区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区域 -->
    <el-card>
      <el-row :gutter="20">
        <el-col :span="10">
          <el-input
            placeholder="请输入内容"
            v-model="queryInfo.query"
            clearable
            @clear="getGoodsList"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getGoodsList"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="toAddUser">添加用户 </el-button>
        </el-col>
      </el-row>
      <!-- 表格区域 -->
      <el-table :data="goodsList" style="width: 100%" border stripe>
        <el-table-column type="index"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称"> </el-table-column>
        <el-table-column prop="goods_price" label="商品价格(元)" width="110">
        </el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="100">
        </el-table-column>
        <el-table-column label="创建时间" width="180">
          <template slot-scope="scope">
            {{ scope.row.add_time | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column prop="date" label="操作" width="150">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="showGoodsEdit(scope.row)"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="deleteGoods(scope.row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="+total"
        background
      >
      </el-pagination>
    </el-card>
    <!-- 商品列表编辑的对话框 -->
    <el-dialog title="修改商品" :visible.sync="dialogVisibleGoodsEdit">
      <el-form ref="form" :model="formEdit">
        <el-form-item label="商品名称" label-width="10%">
          <el-input v-model="formEdit.goods_name" disabled></el-input>
        </el-form-item>
        <el-form-item label="商品价格" label-width="10%">
          <el-input v-model="formEdit.goods_price"></el-input>
        </el-form-item>
        <el-form-item label="商品重量" label-width="10%">
          <el-input v-model="formEdit.goods_weight"></el-input>
        </el-form-item>
        <el-form-item label="商品数量" label-width="10%">
          <el-input v-model="formEdit.goods_number"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisibleGoodsEdit = false">取 消</el-button>
        <el-button type="primary" @click="editGoods">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  created() {
    this.getGoodsList()
  },
  data() {
    return {
      // 商品列表数据
      goodsList: [],
      // 商品列表的请求参数
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      // 商品列表的总数据
      total: '',
      // 商品列表编辑对话框是否显示
      dialogVisibleGoodsEdit: false,
      // 编辑表单存储的数据
      formEdit: {
        goods_name: '',
        goods_price: 0,
        goods_number: 0,
        goods_weight: 0,
        goods_introduce: '',
        pics: [],
        attrs: []
      }
    }
  },
  methods: {
    // 获取商品列表数据
    async getGoodsList() {
      const { data: res } = await this.$http.get('goods', {
        params: this.queryInfo
      })
      console.log(res)
      if (res.meta.status !== 200) {
        return this.$message.error('获取商品列表数据失败')
      }
      this.goodsList = res.data.goods
      this.total = res.data.total
    },
    // 当每页显示数目变化是触发
    handleSizeChange(val) {
      // console.log(val)
      this.queryInfo.pagesize = val
      this.getGoodsList()
    },
    // 档切换页码是触发
    handleCurrentChange(val) {
      // console.log(val)
      this.queryInfo.pagenum = val
      this.getGoodsList()
    },
    // 显示商品编辑列表对话框
    showGoodsEdit(row) {
      this.dialogVisibleGoodsEdit = true
      console.log(row)
      this.formEdit = row
    },

    // 编辑商品列表
    async editGoods() {
      console.log(this.formEdit)
      const { data: res } = await this.$http.put(
        `goods/${this.formEdit.goods_id}`,
        this.formEdit
      )
      console.log(res)
      if (res.meta.status !== 201) {
        return this.$message.error('修改失败')
      }
      this.$message.success('修改成功')
      this.dialogVisibleGoodsEdit = false
      this.getGoodsList()
    },
    // 删除商品
    deleteGoods(goodsRow) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const { data: res } = await this.$http.delete(
            'goods/' + goodsRow.goods_id
          )
          // console.log(res)
          if (res.meta.status !== 200) {
            return this.$message.error('删除失败')
          }
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.getGoodsList()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 监听添加用户按钮
    toAddUser() {
      this.$router.push('goods/add')
    }
  }
}
</script>

<style lang="less" scoped></style>
